// miniprogram/pages/gameRecord/gameRecord.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signUpList: [],
    recordList: [],
    user: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // 获取signUpList
    wx.cloud.callFunction({
      name: "getSignUpRecordByOpenId",
      data: {
        openId: options.openId
      },
      success: res => {
        console.log("调用云函数获取报名记录成功", res)
        that.setData({
          signUpList: res.result.data
        })
        // 获取用户信息
        if (that.data.signUpList.length > 0) {
          that.setData({
            user: that.data.signUpList[0].user
          })
        }

        // 获取比赛记录
        // console.log(that.data.signUpList)
        for(let i = 0; i < that.data.signUpList.length; ++i) {
          if (that.data.signUpList[i].contestant_id != "") {
            // console.log(that.data.signUpList[i].gameTitle, that.data.signUpList[i].contestant_id)
            wx.cloud.callFunction({
              name: "getGameRecordByGameTitleAndConId",
              data: {
                title: that.data.signUpList[i].gameTitle,
                contestant: that.data.signUpList[i].contestant_id
              },
              success: sres => {
                console.log("调用云函数获取比赛记录成功", sres)
                if(sres.result.data.length != 0) {
                  // 将成绩记录添加到recordList
                  // console.log("有成绩")
                  let temp = that.data.recordList
                  temp.push(sres.result.data[0])
                  that.setData({
                    recordList: temp
                  })
                  // console.log(that.data.recordList)
                }
              },
              fail: sres => {
                console.log("调用云函数获取比赛记录失败", sres)
              }
            })
          }
        }
      },
      fail: res => {
        console.log("调用云函数获取报名记录失败", res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})